package com.zodiac.app.szse.parse;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import io.github.jonathanlink.PDFLayoutTextStripper;
import lombok.Getter;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * PDF 转换为 TEXT
 */
@Getter
public class Pdf2TextConverter {
    private File pdf;

    public static Pdf2TextConverter create(File pdf) {
        Preconditions.checkNotNull(pdf);
        if (!pdf.exists() || !pdf.isFile()) {
            throw new RuntimeException("PDF文件不存在 -> " + pdf.getAbsolutePath());
        }
        final Pdf2TextConverter convert = new Pdf2TextConverter();
        convert.pdf = pdf;
        return convert;
    }

    /**
     * 转换
     *
     * @return
     */
    public List<String> convert(ConvertType type) {
        return type.convert(this.pdf);
    }

    /**
     * 转换类型
     */
    public enum ConvertType {
        ITEXT {
            @Override
            List<String> convert(File pdf) {
                try {
                    final PdfReader reader = new PdfReader(pdf.getAbsolutePath());
                    final PdfReaderContentParser parser = new PdfReaderContentParser(reader);
                    final List<String> lines = new ArrayList<>();
                    final int pages = reader.getNumberOfPages();
                    for (int i = 1; i <= pages; i++) {
                        lines.add(parser.processContent(i, new SimpleTextExtractionStrategy()).getResultantText());
                    }
                    return lines;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        },
        PDFBOX {
            @Override
            List<String> convert(File pdf) {
                try {
                    final PDFTextStripper stripper = new PDFTextStripper();
                    return Splitter.on(stripper.getLineSeparator()).splitToList(stripper.getText(PDDocument.load(pdf)));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        },
        PDFLAYOUT {
            @Override
            List<String> convert(File pdf) {
                try {
                    PDFParser pdfParser = new PDFParser(new RandomAccessFile(pdf, "r"));
                    pdfParser.parse();
                    PDDocument doc = new PDDocument(pdfParser.getDocument());
                    PDFTextStripper stripper = new PDFLayoutTextStripper();
                    final String text = stripper.getText(doc);
                    return Splitter.on(stripper.getLineSeparator()).splitToList(text);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };

        private final String filename;

        ConvertType() {
            this.filename = name() + ".txt";
        }

        public String getFilename() {
            return filename;
        }

        abstract List<String> convert(File pdf);
    }
}
